/*根据id获取对象*/
function $(str) {
    return document.getElementById(str);
}

var prov = $('prov');
var city = $('city');
var country = $('country');


/*用于保存当前所选的省市区*/
var current = {
    prov: '',
    city: '',
    country: ''
};

let formData = {
    city:'',
    lat:'',
    lng:'',
    cityCode:''
}
var provs = [];
var citys = [];
var countrys = [];
let map = new Map(), map2 = new Map(), map1 = new Map();
/*自动加载省份列表*/
(function showProv(a) {

    var i = 0, k = 0, j = 0;

    Object.keys(provice).forEach(function(o) {
        //根据字母选择对象
        var zimuobj = provice[o];
        Object.keys(zimuobj).forEach(function(e) {
            //遍历字母中的对象
            //区
            var zone = zimuobj [e];
            ++i;
            praseZOne(zone,i,map)
           //市
            var parent1 = zone[`parent`];
            if(JSON.stringify(parent1) != "{}" && parent1 != undefined) {
                ++k;
                praseZOne(parent1,k,map1)

                //省
                var parent2 = parent1[`parent`];
                if (JSON.stringify(parent2) != "{}" && parent2 != undefined) {
                    ++j;
                    praseZOne(parent2,j,map2)

                }
            }
        });
    });
    console.log(map);
    console.log(map1);
    console.log(map2);
    //初始化省份
    for (let key of map2.keys()){
        var provOpt = document.createElement('option');
        provOpt.innerText = map2.get(key)['name'];
        provOpt.value = key
        prov.appendChild(provOpt);
    };
})();

function praseZOne(obj,i,map){
    if(obj['level'] == "province"){
        if (!map.has(obj['adcode'])) {
            map.set(obj['adcode'], obj);
        }else {i--}
    }else if(obj['level'] == "city"){
        if (!map.has(obj['adcode'])) {
            map.set(obj['adcode'], obj);
        }else {i--}


    }else if(obj['level'] == "district"){
        if (!map.has(obj['adcode'])) {
            map.set(obj['adcode'], obj);
        }else {i--}

    }
}
/*根据所选的省份来显示城市列表*/
function showCity(obj) {
    var val = obj.options[obj.selectedIndex].value;
    if (val != current.prov) {
        current.prov = val;
    }
    //console.log(val);
    if (val != null) {
        city.length = 1;
        for (let key of map1.keys()){
            if(val.substr(0,2) == key.substr(0,2)){
                let cityOpt = document.createElement('option');
                cityOpt.innerText = map1.get(key)['name'];
                cityOpt.value = key
                city.appendChild(cityOpt);
            }
        };
    }
}

/*根据所选的城市来显示县区列表*/
function showCountry(obj) {
    var val = obj.options[obj.selectedIndex].value;
    current.city = val;
    if (val != null) {
        country.length = 1; //清空之前的内容只留第一个默认选项
        for (let key of map.keys()){
            if(val.substr(0,4) == key.substr(0,4)){
                let countryOpt = document.createElement('option');
                countryOpt.innerText = map.get(key)['name'];
                countryOpt.value = key
                country.appendChild(countryOpt);
            }
        };
    }
}

/*选择县区之后的处理函数*/
function selecCountry(obj) {
    current.country = obj.options[obj.selectedIndex].value;
    if ((current.city != null) && (current.country != null)) {
    }
}
